package com.eco.fanliapp.service.impl;

import com.eco.fanliapp.common.response.ServerResponse;
import com.eco.fanliapp.dao.SyTljGoodsfreeMapper;
import com.eco.fanliapp.entity.SyTljGoodsfree;
import com.eco.fanliapp.entity.SyTljGoodsfreeUser;
import com.eco.fanliapp.dao.SyTljGoodsfreeUserMapper;
import com.eco.fanliapp.service.ISyTljGoodsfreeUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eco.fanliapp.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 淘礼金免单商品用户对换表 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2019-11-27
 */
@Service
public class SyTljGoodsfreeUserServiceImpl extends ServiceImpl<SyTljGoodsfreeUserMapper, SyTljGoodsfreeUser> implements ISyTljGoodsfreeUserService {
    @Autowired
    private SyTljGoodsfreeMapper syTljGoodsfreeMapper;
    @Autowired
    private SyTljGoodsfreeUserMapper syTljGoodsfreeUserMapper;

    @Override
    public ServerResponse tljGoodsfreeExchange(Long userId, Long itemId, String code) {
        SyTljGoodsfree syTljGoodsfree = syTljGoodsfreeMapper.selectTljGoodsfreeByItemId(itemId);
        SyTljGoodsfreeUser syTljGoodsfreeUser = syTljGoodsfreeUserMapper.selectTljGoodsfreeUser(userId);
        if(syTljGoodsfree == null){
            return ServerResponse.createBySuccessCodeMessage(201,"没有商品了，明天再来吧！");
        }else {
            if(StringUtils.isNull(code)){
                return ServerResponse.createBySuccessCodeMessage(201,"兑换码不能为空！");
            }
            if(syTljGoodsfreeUser != null){
                return ServerResponse.createBySuccessCodeMessage(201,"您今日的兑换次数用完了，明天再来吧！");
            }
            if(StringUtils.isNotNull(syTljGoodsfree.getExchangeCode()) && syTljGoodsfree.getExchangeCode().equals(code)){
                if(syTljGoodsfree.getFailureTime().compareTo(new Date()) < 0){
                    return ServerResponse.createBySuccessCodeMessage(201,"今日商品兑换结束了，明天再来吧！");
                }
                if(syTljGoodsfree.getExchangeNum()< syTljGoodsfree.getTljNum() && StringUtils.isNotNull(syTljGoodsfree.getItemLink())){
                    SyTljGoodsfreeUser syTljGoodsfreeUserOne = new SyTljGoodsfreeUser();
                    syTljGoodsfreeUserOne.setItemId(itemId);
                    syTljGoodsfreeUserOne.setUserId(userId);
                    syTljGoodsfreeUserOne.setExchangeCode(code);
                    syTljGoodsfreeUserOne.setTljTableId(syTljGoodsfree.getTableId());
                    syTljGoodsfreeUserOne.setBelongDate(new Date());
                    int i = syTljGoodsfreeUserMapper.insert(syTljGoodsfreeUserOne);
                    if(i > 0){
                        //记录用户兑换
                        syTljGoodsfreeMapper.updateNumById(syTljGoodsfree.getTableId());
                        return ServerResponse.createBySuccess(syTljGoodsfree.getItemLink());
                    }
                }else {
                    return ServerResponse.createBySuccessCodeMessage(201,"今日商品兑换结束了，明天再来吧！");
                }
            }else {
                return ServerResponse.createBySuccessCodeMessage(201,"兑换码错误！");
            }
        }

        return ServerResponse.createBySuccessCodeMessage(201,"兑换失败！");
    }
}
